﻿using BootyBay.PrivateStoreApp.AgentServicesRef;
using BootyBay.PrivateStoreApp.DataModel;
using BootyBay.PrivateStoreApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace BootyBay.PrivateStoreApp.Common
{
    #region 根据bool类型判断显隐
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is Boolean && (bool)value)
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value is Visibility && (Visibility)value == Visibility.Visible)
            {
                return true;
            }
            return false;
        }
    }
    #endregion

    #region 根据切换图片索引值判断列表框的背景颜色
    public class BorderMenuConverter : IValueConverter
    {
        string str = "";
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            int count = int.Parse(parameter.ToString());
            if (count == (int)value)
            {
                str = "#C5C5C5";
            }
            else
            {
                str = "#666666";
            }
            return str;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据切换图片的索引值判断图片框的透明度
    public class ImagesMenuConverter : IValueConverter
    {
        int count = -1;
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            count++;
            if (count > AppDetailPageVM.Instance.AppModel.AppView.ScreenshotUrlList.Count - 1)
            {
                count = 0;
            }
            if (count == (int)value)
            {
                return "0.8";
            }
            return "1";
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据类别int值判断类别名称
    public class CategoryNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if ((int)value != 0)
            {
                if (AppCategoriesPageVM.Instance.AppCategories.Count > (int)value)
                {
                    return AppCategoriesPageVM.Instance.AppCategories[(int)value - 1].Value;
                }
                return "";
            }
            else
            {
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据app名称判断app的近似大小
    public class GetAppSizeByAppNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return appSize;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
        StorageFolder RootFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFolder FolderAll = null;
        double appSize = 0;

        private async Task<double> GetMyApps(string appName)
        {
            try
            {
                FolderAll = await RootFolder.GetFolderAsync("AllApps");
            }
            catch (Exception)
            {
            }
            if (FolderAll != null)
            {
                System.Collections.Generic.IReadOnlyList<StorageFile> lists = await FolderAll.GetFilesAsync();
                AppListPageVM applistPageVM = new AppListPageVM();
                foreach (StorageFile item in lists)
                {

                    if (item.Name == appName + ".appx")
                    {
                        BasicProperties basic = await item.GetBasicPropertiesAsync();
                        appSize += basic.Size;
                    }
                    if (item.Name == appName + ".cer")
                    {
                        BasicProperties basic = await item.GetBasicPropertiesAsync();
                        appSize += basic.Size;
                    }
                }
            }
            return appSize;
        }
    }
    #endregion

    #region 处理器类型转换
    public class ProcessorArchitectureConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string strs = "";
            string result = "";
            try
            {
                string[] para = value.ToString().Split(';');
                for (int i = 0; i < para.Length; i++)
                {
                    if (para[i] != "")
                    {

                        result += ((ProcessorArchitecture)int.Parse(para[i])).ToString() + " , ";
                    }
                }
                string str = result.Substring(result.Length - 3);
                if (str == " , ")
                {
                    strs = result.Substring(0, result.Length - 3);
                }
            }
            catch (Exception)
            {
            }
            if (strs == "Neutral")
            {
                strs = "Any CPU";
            }
            return strs;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 应用详细页面中应用的图片列表索引
    public class ScreenshotUrlListConverter : IValueConverter
    {
        int count = 0;
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            count++;
            return count - 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据图片路径判断是否显示显示默认图片
    public class IsShowDefaultPicConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string strs = "";
            if (parameter != null)
            {
                strs = parameter.ToString();
            }
            if (value != null)
            {
                if (value.ToString() != "")
                {
                    strs = value.ToString();
                }
            }
            return strs;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据150*150图片判断310*150图片是否显示
    public class IsShow310_150PicConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool bo = bool.Parse(parameter.ToString());
            Visibility vis = Visibility.Visible;
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                if (bo)
                {
                    vis = Visibility.Visible;
                }
                else
                {
                    vis = Visibility.Collapsed;
                }
            }
            else
            {
                if (bo)
                {
                    vis = Visibility.Collapsed;
                }
                else
                {
                    vis = Visibility.Visible;
                }
            }
            return vis;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return false;
        }
    }
    #endregion

    #region 根据字符串参数是否为空判断显隐
    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if ((string)value == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return true;
        }
    }
    #endregion

    #region 根据字符串参数判断按钮是否可以点击
    public class StringToHitButtonConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if ((string)value == "Run")
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return true;
        }
    }
    #endregion

    #region 根据字符串参数判断按钮上显示的内容
    public class StringToShowNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if ((string)value == "Run")
            {
                return "Installed";
            }
            else
            {
                return (string)value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return true;
        }
    }
    #endregion

    #region 根据推荐列表的高度判断类别列表的高度
    public class RecommendListToCategoryListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (int.Parse(value.ToString()) > 0)
            {
                return int.Parse(value.ToString()) - 200;
            }
            else
            {
                return int.Parse(value.ToString());
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return int.Parse(value.ToString());
        }
    }
    #endregion

}
